package com.dayatang.android.Adapter;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.dayatang.android.Activity.R;
import com.dayatang.android.Activity.ProjectDetailActivity;
import com.dayatang.android.config.Config;

public class ProjectListViewAdapter extends BaseAdapter {
	
	private Context context;
	private List<HashMap<String, Object>> listItem;
	private LayoutInflater listContainer;           

	class ListItemView {
		private Button enterBtn;
		private TextView projectNameTextView;
		private TextView projectStateTextView;
		private TextView projectBudgetTextView;
		private TextView projectIdTextView;
	}
	
	public ProjectListViewAdapter(Context context, List<HashMap<String, Object>> listItem) {
		this.context = context;
		this.listItem = listItem;
		listContainer = LayoutInflater.from(context);   
	}

	public int getCount() {
		return listItem.size();
	}

	public Object getItem(int position) {
		return listItem.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		ListItemView  listItemView = null;
		
		if (convertView == null) {
			listItemView = new ListItemView();
			convertView = listContainer.inflate(R.layout.search_listview_item, null);
			
			listItemView.enterBtn = (Button)convertView.findViewById(R.id.enter_btn);
			listItemView.projectNameTextView = (TextView)convertView.findViewById(R.id.search_listview_item_projectname_textview);
			listItemView.projectStateTextView = (TextView)convertView.findViewById(R.id.search_listview_project_state_textview);
			listItemView.projectBudgetTextView = (TextView)convertView.findViewById(R.id.search_listview_project_budget_amount_textview);
			listItemView.projectIdTextView = (TextView)convertView.findViewById(R.id.project_list_project_id);
			
            convertView.setTag(listItemView);    

		} else {    
            listItemView = (ListItemView)convertView.getTag();    
        }  
		
		listItemView.projectNameTextView.setText((String)listItem.get(position).get("ItemProjectName"));
		listItemView.projectStateTextView.setText((String)listItem.get(position).get("ItemProjectState"));
		listItemView.projectBudgetTextView.setText((String)listItem.get(position).get("ItemProjectBudget"));
		listItemView.projectIdTextView.setText((String)listItem.get(position).get("ItemProjectId"));
		
		final String projectid = (String)listItem.get(position).get("ItemProjectId");
		listItemView.enterBtn.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View arg0) {
				Intent intent = new Intent(context, ProjectDetailActivity.class);
				intent.putExtra(Config.PROJID, projectid);
				intent.putExtra(Config.FROMWHERE, "log");
				context.startActivity(intent);
			}
		});
		
		return convertView;
	}

}
